package com.sunny.shop.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Converter
public class StringAndJson implements AttributeConverter<List<String>, String> {
    @Autowired
    private ObjectMapper mapper;

    /**
     * 模型字段转换成数据库字段
     * @param strings
     * @return
     */
    @Override
    public String convertToDatabaseColumn(List<String> strings) {
        return "1";
    }

    /**
     * 数据库字段转换成模型字段
     * @param s
     * @return
     */
    @Override
    public List<String> convertToEntityAttribute(String s) {
        Map<Integer,String> storyTag = new HashMap<>();
        storyTag.put(1,"励志成长");
        storyTag.put(2,"友情");
        storyTag.put(3,"冒险");
        storyTag.put(4,"启蒙");
        storyTag.put(5,"家庭教育");
        storyTag.put(6,"情绪管理");
        storyTag.put(7,"行为养成");
        storyTag.put(8,"智力提升");
        List<String> tagList = new ArrayList<>();
        String[] tagArr = s.split(",");
        for (int i = 0; i < tagArr.length; i++) {
            Integer key = Integer.valueOf(tagArr[i]);
            String tag = storyTag.get(key);
            tagList.add(tag);
        }
        return tagList;
    }
}
